package common.zsxmlv.utils;


import common.zsxmlv.vo.Device;

import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: ZSXM
 * @Description: 工具类
 * @Company: DingZheng
 * @Date: Create by 2018/2/6 11:52
 * @Version: 1.0
 * @Modify by:
 */
public class Utils {

    /**
     * 生成32位唯一字符串
     * @return 随机字符串 32位
     */
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
    }

    /**
     * 生成文件名字
     * @return 日期_时间戳(yyyyMMdd_1527068342753)
     */
    public static String generateFileName(){
        return new SimpleDateFormat("yyyyMMdd").format(new Date())+"_"+System.currentTimeMillis()+".txt";
    }

    /**
     * 判断对象是否有属性为空值
     * @param object
     * @return
     * @throws IllegalAccessException
     */
    public static boolean checkObjectFidldIsNull(Object object) throws IllegalAccessException {

        for (Field field : object.getClass().getDeclaredFields()){
            field.setAccessible(true);
            System.out.println("字段名称："+field.getName());
            if (field.get(object) == null){
                return true;
            }
        }
        return false;
    }

    /**
     * String[] parse int[]
     * @param arrs
     * @return
     */
    public static int[] stringToInt(String[] arrs){
        int[] ints = new int[arrs.length];
        for (int i = 0; i < arrs.length; i++) {
            ints[i] = Integer.parseInt(arrs[i]);
        }
        return ints;
    }

    /**
     * 获取用户设备信息
     * @param userAgent 浏览器标志
     * @return 设备信息
     */
    public static Device getOS(String userAgent){
        userAgent = userAgent.toLowerCase();
        boolean android = userAgent.indexOf("android")==-1?false:true;
        String os = userAgent.indexOf("windows") > -1 ? "windows" :
                userAgent.indexOf("linux") > -1 ? "android" :
                        userAgent.indexOf("iphone") > -1 || userAgent.indexOf("ipod") > -1 || userAgent.indexOf("ipad") > -1 || userAgent.indexOf("ios") > -1 ? "ios" :
                                "其他设备";
        boolean ios = os.indexOf("ios") > -1;
        boolean isWeixin = userAgent.indexOf("micromessenger")>-1;
        return new Device(android, ios, os, isWeixin);
    }

    /**
     * windows 调用可执行文件.exe
     * @param location .exe 文件位置或者是 command 命令
     * @throws IOException
     */
    public static void openExe(String location) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(location);
    }



    /**
     * 获取时间 yyyy-MM-dd HH:mm:ss.SSS
     * @return
     */
    public static String getTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    public static void main(String[] args) {
        try {
            Utils.openExe("E:\\develop\\python\\Demo\\dist\\selenium_test.exe");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("调用异常");
        }
    }

}
